package cn.donghuapian.consumer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @description:
 * @author: hzw
 * @time: 2023/10/12 10:01
 */
@Component
public class DelayQueue {
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "delayQueue", arguments = {
                    @Argument(name = "x-dead-letter-exchange", value = "deadExchange"),
                    @Argument(name = "x-dead-letter-routing-key", value = "deadRoutingKey"),
                    @Argument(name = "x-message-ttl", value = "8000", type = "java.lang.Long")
            }),
            exchange = @Exchange(value = "delayExchange"),
            key = "delayRoutingKey"
    ))
    public void normalListener(String msg, Channel channel, Message message) throws IOException {

        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            Thread.sleep(5000l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("正常消费消息：" + msg);
        channel.basicAck(deliveryTag, false);
    }

}
